﻿using System;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Common.Logging;
using WuDada.Core.Post;
using WuDada.Core.Post.Domain;
using WuDada.Core.Post.Service;
using WuDada.Core.SystemApplications.Domain;
using System.Collections.Generic;

public partial class admin_UC02_0222 : System.Web.UI.Page
{
    private ILog m_Log = LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
    private PostFactory m_PostFactory;
    private IPostService m_PostService;
    private WebLogService m_WebLogService;

    //加入我們分類 ParentNodeId=38
    private int m_ParentNodeId = 38;

    private int m_Mode
    {
        get { if (ViewState["mode"] == null) { ViewState["mode"] = 0; } return int.Parse(ViewState["mode"].ToString()); }
        set { ViewState["mode"] = value; }
    }
    private string m_PicFileName
    {
        get { if (ViewState["picfilename"] == null) { ViewState["picfilename"] = string.Empty; } return ViewState["picfilename"].ToString(); }
        set { ViewState["picfilename"] = value; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        m_WebLogService = new WebLogService();
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            pnlContent.Visible = false;
            btnSearch_Click(null, null);
            ShowMode();
        }
    }

    private void ShowMode()
    {
        if (m_Mode == 0)
        {
            btnSave.Visible = false;
        }
        else
        {
            btnSave.Visible = true;
        }
    }

    private void fillGridView()
    {
        IDictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("ParentNodeId", m_ParentNodeId.ToString());
        conditions.Add("Order", "order by p.CreatedDate desc ");

        if (!string.IsNullOrEmpty(ddlSearchNode.SelectedValue))
        {
            conditions.Add("NodeId", ddlSearchNode.SelectedValue);
        }

        if (!string.IsNullOrEmpty(ddlSearchType.SelectedValue))
        {
            conditions.Add("Type", ddlSearchType.SelectedValue);
        }

        //分頁
        AspNetPager1.RecordCount = m_PostService.GetPostCount(conditions);
        conditions.Add("PageIndex", (AspNetPager1.CurrentPageIndex - 1).ToString());
        conditions.Add("PageSize", AspNetPager1.PageSize.ToString());

        gvList.DataSource = m_PostService.GetPostList(conditions);
        gvList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        fillGridView();
    }    

    private void ClearUI()
    {
        UIHelper.ClearUI(pnlContent);
        m_Mode = 0;
        m_PicFileName = string.Empty;
        ddlType.SelectedValue = string.Empty;
        pnlContent.Visible = false;
    }

    protected void gvList_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        string cmdName = e.CommandName;
        int postId = int.Parse(e.CommandArgument.ToString());
        PostVO postVO = m_PostService.GetPostById(postId);
        NodeVO nodeVO = m_PostService.GetNodeById(postVO.Node.NodeId);
        switch (cmdName)
        {
            case "myModify":
                ClearUI();
                UIHelper.FillUI(pnlContent, postVO);
                m_Mode = postId;                
                ddlType.SelectedValue = postVO.Type.ToString();
                lblNodeName.Text = nodeVO.Name;
                lblCreateDate.Text = postVO.CreatedDate.ToString();
                if (!string.IsNullOrEmpty(postVO.HtmlContent))
                {
                    lblHtmlContent.Text = postVO.HtmlContent.Replace(Environment.NewLine, "<br>");
                }
                ShowMode();
                pnlContent.Visible = true;
                btnSearch.Enabled = false;
                ddlSearchType.Enabled = false;
                break;
            case "myDel":
                m_PostService.DeletePost(postVO);
                m_WebLogService.AddSystemLog(MsgVO.Action.刪除, postVO);
                break;

            default:
                break;
        }
        btnSearch_Click(null, null);
    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        ddlSearchType.Enabled = true;
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        fillGridView();
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        PostVO postVO = m_PostService.GetPostById(m_Mode);
        postVO.Type = int.Parse(ddlType.SelectedValue.Trim());
        WebLogService.SetUpdateInfo(postVO);
        m_PostService.UpdatePost(postVO);
        m_WebLogService.AddSystemLog(MsgVO.Action.修改, postVO);
        fillGridView();
        ClearUI();
        ShowMode();
        btnSearch.Enabled = true;
        ddlSearchType.Enabled = true;
    }

    protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView gv = (GridView)sender;
        if (e.Row.RowIndex != -1)
        {
            Control ctrl = e.Row;
            int postId = int.Parse(UIHelper.FindHiddenValue(ref ctrl, "hdnPostId"));
            PostVO postVO = m_PostService.GetPostById(postId);
            if (1.Equals(postVO.Type))
            {
                UIHelper.SetLabelText(ref ctrl, "lblType", "已處理");
            }
            else
            {
                UIHelper.SetLabelText(ref ctrl, "lblType", "未處理");
            }

            NodeVO nodeVO = m_PostService.GetNodeById(postVO.Node.NodeId);
            UIHelper.SetLabelText(ref ctrl, "lblNodeName", nodeVO.Name);
        }
    }

    protected void btnShowAdd_Click(object sender, EventArgs e)
    {
        ClearUI();
        m_PicFileName = string.Empty;
        ShowMode();
        pnlContent.Visible = true;
        btnSearch.Enabled = false;
    }
}